﻿using System;
using UnityEngine;

namespace Sound
{
	// Token: 0x0200034D RID: 845
	[RequireComponent(typeof(AudioSource))]
	public class FadePlayer : MonoBehaviour
	{
		// Token: 0x17000220 RID: 544
		// (get) Token: 0x0600154F RID: 5455 RVA: 0x0009189C File Offset: 0x0008FA9C
		// (set) Token: 0x06001550 RID: 5456 RVA: 0x000918A4 File Offset: 0x0008FAA4
		public AudioSource audioSource { get; private set; }

		// Token: 0x06001551 RID: 5457 RVA: 0x000918B0 File Offset: 0x0008FAB0
		private void Awake()
		{
			this.audioSource = base.GetComponent<AudioSource>();
			this.state = new FadePlayer.Wait(this);
		}

		// Token: 0x06001552 RID: 5458 RVA: 0x000918CC File Offset: 0x0008FACC
		private void Update()
		{
			this.state.Update();
			if (this.state is FadePlayer.Wait && !this.audioSource.isPlaying)
			{
				UnityEngine.Object.Destroy(base.gameObject);
			}
		}

		// Token: 0x06001553 RID: 5459 RVA: 0x00091908 File Offset: 0x0008FB08
		public void Play(float fadeTime = 0f)
		{
			this.fadeInTime = fadeTime;
			this.state.Play();
		}

		// Token: 0x06001554 RID: 5460 RVA: 0x0009191C File Offset: 0x0008FB1C
		public void Pause()
		{
			this.state.Pause();
		}

		// Token: 0x06001555 RID: 5461 RVA: 0x0009192C File Offset: 0x0008FB2C
		public void Stop(float fadeTime)
		{
			this.fadeOutTime = fadeTime;
			this.state.Stop();
		}

		// Token: 0x04001977 RID: 6519
		public float currentVolume = 1f;

		// Token: 0x04001979 RID: 6521
		private FadePlayer.State state;

		// Token: 0x0400197A RID: 6522
		private float fadeInTime;

		// Token: 0x0400197B RID: 6523
		private float fadeOutTime;

		// Token: 0x0200034E RID: 846
		public abstract class State
		{
			// Token: 0x06001556 RID: 5462 RVA: 0x00091940 File Offset: 0x0008FB40
			public State(FadePlayer player)
			{
				this.player = player;
			}

			// Token: 0x06001557 RID: 5463 RVA: 0x00091950 File Offset: 0x0008FB50
			public virtual void Play()
			{
			}

			// Token: 0x06001558 RID: 5464 RVA: 0x00091954 File Offset: 0x0008FB54
			public virtual void Pause()
			{
			}

			// Token: 0x06001559 RID: 5465 RVA: 0x00091958 File Offset: 0x0008FB58
			public virtual void Stop()
			{
			}

			// Token: 0x0600155A RID: 5466 RVA: 0x0009195C File Offset: 0x0008FB5C
			public virtual bool Update()
			{
				return true;
			}

			// Token: 0x0400197C RID: 6524
			protected FadePlayer player;
		}

		// Token: 0x0200034F RID: 847
		public class Wait : FadePlayer.State
		{
			// Token: 0x0600155B RID: 5467 RVA: 0x00091960 File Offset: 0x0008FB60
			public Wait(FadePlayer player) : base(player)
			{
			}

			// Token: 0x0600155C RID: 5468 RVA: 0x0009196C File Offset: 0x0008FB6C
			public override void Play()
			{
				if (this.player.fadeInTime > 0f)
				{
					this.player.state = new FadePlayer.FadeIn(this.player);
				}
				else
				{
					this.player.state = new FadePlayer.Playing(this.player);
				}
			}
		}

		// Token: 0x02000350 RID: 848
		public class FadeIn : FadePlayer.State
		{
			// Token: 0x0600155D RID: 5469 RVA: 0x000919C0 File Offset: 0x0008FBC0
			public FadeIn(FadePlayer player) : base(player)
			{
				player.audioSource.Play();
				player.audioSource.volume = 0f;
			}

			// Token: 0x0600155E RID: 5470 RVA: 0x000919E4 File Offset: 0x0008FBE4
			public override void Pause()
			{
				this.player.state = new FadePlayer.Paused(this.player);
			}

			// Token: 0x0600155F RID: 5471 RVA: 0x000919FC File Offset: 0x0008FBFC
			public override void Stop()
			{
				this.player.state = new FadePlayer.FadeOut(this.player);
			}

			// Token: 0x06001560 RID: 5472 RVA: 0x00091A14 File Offset: 0x0008FC14
			public override bool Update()
			{
				this.t += Time.deltaTime;
				this.player.audioSource.volume = Mathf.Lerp(0f, this.player.currentVolume, this.t / this.player.fadeInTime);
				if (this.t >= this.player.fadeInTime)
				{
					this.player.state = new FadePlayer.Playing(this.player);
					return true;
				}
				return false;
			}

			// Token: 0x0400197D RID: 6525
			private float t;
		}

		// Token: 0x02000351 RID: 849
		public class Playing : FadePlayer.State
		{
			// Token: 0x06001561 RID: 5473 RVA: 0x00091A9C File Offset: 0x0008FC9C
			public Playing(FadePlayer player) : base(player)
			{
				if (!player.audioSource.isPlaying)
				{
					player.audioSource.Play();
				}
			}

			// Token: 0x06001562 RID: 5474 RVA: 0x00091AC0 File Offset: 0x0008FCC0
			public override void Pause()
			{
				this.player.state = new FadePlayer.Paused(this.player);
			}

			// Token: 0x06001563 RID: 5475 RVA: 0x00091AD8 File Offset: 0x0008FCD8
			public override void Stop()
			{
				this.player.state = new FadePlayer.FadeOut(this.player);
			}
		}

		// Token: 0x02000352 RID: 850
		public class Paused : FadePlayer.State
		{
			// Token: 0x06001564 RID: 5476 RVA: 0x00091AF0 File Offset: 0x0008FCF0
			public Paused(FadePlayer player) : base(player)
			{
				this.preState = player.state;
				player.audioSource.Pause();
			}

			// Token: 0x06001565 RID: 5477 RVA: 0x00091B10 File Offset: 0x0008FD10
			public override void Stop()
			{
				this.player.audioSource.Stop();
				this.player.state = new FadePlayer.Wait(this.player);
			}

			// Token: 0x06001566 RID: 5478 RVA: 0x00091B38 File Offset: 0x0008FD38
			public override void Play()
			{
				this.player.state = this.preState;
				this.player.audioSource.Play();
			}

			// Token: 0x0400197E RID: 6526
			private FadePlayer.State preState;
		}

		// Token: 0x02000353 RID: 851
		public class FadeOut : FadePlayer.State
		{
			// Token: 0x06001567 RID: 5479 RVA: 0x00091B5C File Offset: 0x0008FD5C
			public FadeOut(FadePlayer player) : base(player)
			{
				player.currentVolume = player.audioSource.volume;
			}

			// Token: 0x06001568 RID: 5480 RVA: 0x00091B78 File Offset: 0x0008FD78
			public override void Pause()
			{
				this.player.state = new FadePlayer.Paused(this.player);
			}

			// Token: 0x06001569 RID: 5481 RVA: 0x00091B90 File Offset: 0x0008FD90
			public override bool Update()
			{
				this.t += Time.deltaTime;
				this.player.audioSource.volume = this.player.currentVolume * (1f - this.t / this.player.fadeOutTime);
				if (this.t >= this.player.fadeOutTime)
				{
					this.player.audioSource.volume = 0f;
					this.player.audioSource.Stop();
					this.player.state = new FadePlayer.Wait(this.player);
					return true;
				}
				return false;
			}

			// Token: 0x0400197F RID: 6527
			private float t;
		}
	}
}
